﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CustomizeControl
{
    public class CustomDateTimePicker : Control
    {
        private DateTime _selectedDate = DateTime.Now;
        private bool _isCalendarOpen = false;
        private CalendarPopup _calendarPopup;

        public CustomDateTimePicker()
        {
            DoubleBuffered = true;
            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            _calendarPopup = new CalendarPopup(this);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                _isCalendarOpen = !_isCalendarPopupOpen;
                if (_isCalendarOpen)
                    _calendarPopup.Show(this);
                else
                    _calendarPopup.Hide();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            // 绘制边框、背景和日期文本
            var g = e.Graphics;
            g.Clear(BackColor);
            using (var pen = new Pen(Color.LightGray))
            {
                g.DrawRectangle(pen, ClientRectangle);
            }
            using (var font = new Font("Segoe UI", 10))
            {
                g.DrawString(_selectedDate.ToString("yyyy-MM-dd"), font, Brushes.Black, new PointF(5, 5));
            }
        }

        // 属性绑定
        public DateTime SelectedDate
        {
            get => _selectedDate;
            set
            {
                _selectedDate = value;
                Invalidate(); // 触发重绘
            }
        }
    }



}
